//
//  HXTabBar.swift
//  DemoSinaWeibo
//
//  Created by Mon on 15/11/23.
//  Copyright © 2015年 Mon. All rights reserved.
//

import UIKit

class HXTabBar: UITabBar {
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let width = bounds.size.width / 5
        
        let frame = CGRect(x: 0, y: 0, width: width, height: bounds.size.height)
        
        //记录下标变化
        var index = 0
        //重设tabBarItem的位置
        for view in self.subviews
        {
            //需要判断tabBar中的子控件的类型,不能判断tabBarButton(此类为苹果私有的),需要判断父类是否一致
            //这个位置需要添加一个判断条件,因为“自定义撰写的按钮不需要偏移,始终在中间位置就可以了”
            if view is UIControl && !(view is UIButton)
            {
                view.frame = CGRectOffset(frame, CGFloat(index) * width, 0)
                
                index += index == 1 ? 2 : 1
            }
        }
        
        composeButton.frame = CGRectOffset(frame, 2 * width, 0)
    }
    
    //懒加载撰写按钮
    lazy var composeButton: UIButton = {
        let button = UIButton()
        
        //设置按钮背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置按钮加号图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //把按钮添加到tabBar中
        self.addSubview(button)
        
        return button
    }()

}
